/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.template.client;

import java.util.List;

import com.cleangwt.bootstrap.client.ui.Breadcrumb;
import com.cleangwt.bootstrap.client.ui.Navbar;
import com.cleangwt.bootstrap.client.ui.PageNavigator;
import com.cleangwt.page.client.PageContainer;
import com.cleangwt.user.shared.Link;
import com.cleangwt.user.shared.Links;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A {@link NavbarFixedContainer} with a fixed-top {@link Navbar} that contains a
 * {@link PageNavigator}.
 * 
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class NavbarAffixedContainer3 extends Composite implements PageContainer {

  private DockPanel layout = new DockPanel();
  private Navbar navbar = new Navbar();
  private SimplePanel viewport = new SimplePanel();
  private PageNavigator navigator = new PageNavigator();
  private Breadcrumb breadcrumbs = new Breadcrumb();
  private List<Link> links;

  /**
   * Creates a navbar-affixed page container with the given navbar style and links.
   * 
   * @param navbarStyle the navbar's type style
   * @param links the links
   */
  public NavbarAffixedContainer3(String navbarStyle, List<Link> links) {
    this.links = links;
    initWidget(layout);
    setWidth("100%");

    layout.add(navbar, DockPanel.NORTH);
    layout.add(breadcrumbs, DockPanel.NORTH);
    layout.add(viewport, DockPanel.CENTER);
    navbar.setStyleBase(navbarStyle);
    navbar.addStyleName(Navbar.FIXED_TOP_STYLE);
    this.navigator.setLinks(links);
    this.navbar.addNavigator(navigator);

    breadcrumbs.getElement().getStyle().setMarginTop(50, Unit.PX);
  }

  @Override
  public void setContentPage(String pageName, Widget page) {
    viewport.setWidget(page);
    List<Link> nodes = Links.getLinkedNodes(links, pageName);

    navigator.setActivePage(pageName);
    breadcrumbs.setLinks(nodes);
  }

  /**
   * Gets the navbar.
   * 
   * @return the navbar
   */
  public Navbar getNavbar() {
    return navbar;
  }

  @Override
  public void clearPage() {
    viewport.setWidget(null);
  }

}
